package com.j2some;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.alibaba.fastjson.JSONObject;
import com.yinquan.util.HttpGet;
import com.yishun.springboot.Application;
import com.yishun.springboot.mapper.GwsCWRMapper;
import com.yishun.springboot.mapper.YzwWorkerListMapper;
import com.yishun.springboot.pojo.YzwWorkerList;

import lombok.extern.slf4j.Slf4j;

/**
 * 过程测试 通过图片的URI保存图片到本地 -- 云筑网实名制照片（近照及头像照）
 * 
 * @date 2021年5月7日 上午10:42:22
 * @author XiaoHuang
 *
 */
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class ProcessTestGetPhotoByURI {

	@Autowired
	GwsCWRMapper gwsCWRMapper;

	@Autowired
	YzwWorkerListMapper yzwWorkerListMapper;

	@Test
	public void uploadPhotoAndStoreToProjectPersonnel() {
		// Integer yzwProjectCode = 325437;//十五高
		Integer yzwProjectCode = 339129;// 十三高
		// 所有人员信息
		List<YzwWorkerList> wl = gwsCWRMapper.selectPresenceOfWorkerList(yzwProjectCode, "iDCardNumber");
		HttpGet httpGet = new HttpGet();

		String headImagePath = null;
		String issueCardPicPath = null;

		String localhostHeadImagePath = null;
		String localhostIssueCardPicPath = null;
		String localhostBaseUrl = "D:\\uploadGwsStoreYzwImg\\yzwworkerlist\\";
		int count = 0;

		int catchInt = 0;
		JSONObject catchJSON = new JSONObject();
		List catchArray = new ArrayList();

		File file = new File(localhostBaseUrl);
		if (!file.exists()) { // 判断文件路径是否存在
			file.mkdirs(); // 不存在创建新的文件
		}

		for (YzwWorkerList yzwWorkerList : wl) {
			headImagePath = "";
			issueCardPicPath = "";

			localhostHeadImagePath = "";
			localhostIssueCardPicPath = "";

			// 获取头像照网络地址
			headImagePath = yzwWorkerList.getHeadImagePath();
			// 获取近身照网络地址
			issueCardPicPath = yzwWorkerList.getIssueCardPicPath();

			try {
				// 解析成为本地路径的文件名
				localhostHeadImagePath = headImagePath.substring(headImagePath.lastIndexOf("/") + 1);
				localhostIssueCardPicPath = issueCardPicPath.substring(headImagePath.lastIndexOf("/") + 1);
			} catch (Exception e) {
				catchInt++;
				continue;

			}

			localhostHeadImagePath = localhostBaseUrl + localhostHeadImagePath;
			localhostIssueCardPicPath = localhostBaseUrl + localhostIssueCardPicPath;

			// 添加至下载列表
			httpGet.addItem(headImagePath, localhostHeadImagePath);
			httpGet.addItem(issueCardPicPath, localhostIssueCardPicPath);
			// 开始下载
			httpGet.downLoadByList();
			httpGet.resetList();

			// 下载完成更新表记录
			count += gwsCWRMapper.updateLocalImageByIDCardNumber(localhostHeadImagePath, localhostIssueCardPicPath,
					yzwWorkerList.getIDCardNumber());
			log.info("完成/总计：" + count + "/" + wl.size());

			catchJSON.put("localhostHeadImagePath为空,个数：", catchInt);
			catchArray.add(catchJSON);
			log.error("失败/总计：" + catchInt + "/" + wl.size());
		}

	}

	public void uploadPhotoAndStoreToAlonePersonnel(String iDCardNumber) {
		iDCardNumber = "";
		// 单个人员信息
		YzwWorkerList yzwWorker = yzwWorkerListMapper.findOneRecordByIdCardNumber(iDCardNumber, null);
		HttpGet httpGet = new HttpGet();

		String headImagePath = null;
		String issueCardPicPath = null;

		String localhostHeadImagePath = null;
		String localhostIssueCardPicPath = null;
		String localhostBaseUrl = "D:\\uploadGwsStoreYzwImg\\yzwworkerlist\\";
		int count = 0;

		int catchInt = 0;
		JSONObject catchJSON = new JSONObject();
		List catchArray = new ArrayList();

		File file = new File(localhostBaseUrl);
		if (!file.exists()) { // 判断文件路径是否存在
			file.mkdirs(); // 不存在创建新的文件
		}

		headImagePath = "";
		issueCardPicPath = "";

		localhostHeadImagePath = "";
		localhostIssueCardPicPath = "";

		// 获取头像照网络地址
		headImagePath = yzwWorker.getHeadImagePath();
		// 获取近身照网络地址
		issueCardPicPath = yzwWorker.getIssueCardPicPath();

		try {
			// 解析成为本地路径的文件名
			localhostHeadImagePath = headImagePath.substring(headImagePath.lastIndexOf("/") + 1);
			localhostIssueCardPicPath = issueCardPicPath.substring(headImagePath.lastIndexOf("/") + 1);
		} catch (Exception e) {
			catchInt++;
		}

		localhostHeadImagePath = localhostBaseUrl + localhostHeadImagePath;
		localhostIssueCardPicPath = localhostBaseUrl + localhostIssueCardPicPath;

		// 添加至下载列表
		httpGet.addItem(headImagePath, localhostHeadImagePath);
		httpGet.addItem(issueCardPicPath, localhostIssueCardPicPath);
		// 开始下载
		httpGet.downLoadByList();
		httpGet.resetList();

		// 下载完成更新表记录
		count += gwsCWRMapper.updateLocalImageByIDCardNumber(localhostHeadImagePath, localhostIssueCardPicPath,
				yzwWorker.getIDCardNumber());
		log.info("完成/总计：" + count + "/" + 1);

		catchJSON.put("localhostHeadImagePath为空,个数：", catchInt);
		catchArray.add(catchJSON);
		log.error("失败/总计：" + catchInt + "/" + 1);

	}
}